<?php
    require_once('db.php'); //Require/include the file with the database connection information
     
    // If the mime content type function does not exist, fake it
    if (! function_exists ('mime_content_type' ) ) {
        function mime_content_type ($f ) {
            return system (trim('file -bi ' . escapeshellarg ($f ) ) ) ;
        }
    }
    //Connect to DB using db.php
    $dbf = new my_db;
     
    function addfile ($filepath, $filename) {
        global $dbf;
        $file = "$filepath$filename"; //to make the rest of the code easier we define $file by combining the path with the filename
        $lastrevdate = date("Y-m-d h:i:s"); //get the current date/time
        if (!empty($_SESSION['username'])) {
            $lastuser = $_SESSION['username']; //find out what user is adding the file
        } else {
            $lastuser = 'Unknown';
        }
        if (!file_exists($file)) //check to make sure the file exists, if it does not die with an error
        {
            $status = 'badfile';
        } elseif(!is_readable($file)) {
            $status = 'cannotread';
        } elseif(is_dir($file)) {
            $status = 'isadir';
        } else {
            $openedfile = file_get_contents("$file"); // Set $openedfile to equal the contents of the file
            $filehash = md5_file($file); //calculate the md5 hash of the file
            $contents = addslashes($openedfile); //Make the file safe for adding to the DB
            if (!empty($filenotes)) {
                 
            }
            $revsummary = "Added to Database"; //The default revision summary
            $filetype = mime_content_type("$file"); //Find the MIME type of the file
            $mime = explode("/", $filetype);
             
            //See if the file is already in DB
            $dbf->query("SELECT id FROM trackedfiles WHERE filename = '$filename' AND filepath = '$filepath'");
            //If it is, die with an error
            if ($dbf->nf() == 1) {
                $status = 'already';
            } else {
                //Add it to the DB if it is not already
                $dbf->query("INSERT INTO trackedfiles (filename, filepath, latestrev, lastrevdate, lastuser, filetype) VALUES('$filename','$filepath','$filehash','$lastrevdate', '$lastuser', '$filetype') ");
                $dbf->query("INSERT INTO history (filename, filepath, revhash, revdate, user, revsummary) VALUES('$filename','$filepath','$filehash','$lastrevdate', '$lastuser','$revsummary') ");
                //if the file is a text file (based upon mime type) and it is not already in the DB insert it into the content table
                if ($mime['0'] == "text") {
                    $dbf->query("SELECT dataid FROM content WHERE dataid = '$filehash' ");
                    if ($dbf->nf() == 0) {
                        $dbf->query("INSERT INTO content (dataid, content) VALUES('$filehash','$contents') ");
                    }
                    $status = 'added';
                }
                //if the file is not a text file as determined by its mime type and it is not already in the DB insert it into the binary content table
                else
                    {
                    $dbf->query("SELECT dataid FROM binarycontent WHERE dataid = '$filehash' ");
                    if (!$dbf->nf() == 1) {
                        $dbf->query("INSERT INTO binarycontent (dataid, content, filetype) VALUES('$filehash','$contents','$filetype') ");
                    }
                    $status = 'added';
                }
            }
        }
        return $status;
    }
     
    function updatefilestatus ($latestrev, $file) {
        if (!file_exists($file)) {
            $status = 'badfile';
        } elseif(!is_readable($file)) {
            $status = 'cannotread';
        } elseif(is_dir($file)) {
            $status = 'isdir';
        } else {
            $newhash = md5_file($file);
            if ($newhash !== $latestrev) {
                $status = $newhash;
                 
            } else {
                $status = 'nochange';
            }
        }
        return $status;
         
    }
     
    function updatefile ($id, $newhash, $revsummary, $filepath, $filename) {
        global $dbf;
         
        if (!file_exists("$filepath$filename")) {
            $status = "File does not exist!";
        } elseif(!is_readable("$filepath$filename")) {
            $status = "Can not read file, permission denied!";
             
        } else {
            $openedfile = file_get_contents("$filepath$filename");
            $contents = addslashes($openedfile);
            $lastrevdate = date("Y-m-d h:i:s"); //get the current date/time
            $lastuser = $_SESSION['username'];
            if (empty($lastuser)) {
                $lastuser = 'Unknown';
            }
            $filetype = mime_content_type("$filepath$filename");
            $mime = explode("/", $filetype);
             
            $dbf->query("INSERT INTO history (filename, filepath, revhash, revdate, user, revsummary) VALUES('$filename','$filepath','$newhash','$lastrevdate', '$lastuser','$revsummary') ");
            $dbf->query("UPDATE trackedfiles SET latestrev='$newhash', lastrevdate='$lastrevdate', lastuser='$lastuser' WHERE id = '$id'");
             
            if ($mime['0'] == "text") {
                $dbf->query("SELECT dataid FROM content WHERE dataid = '$newhash' ");
                if ($dbf->nf() == 0) {
                    $dbf->query("INSERT INTO content (dataid, content) VALUES('$newhash','$contents') ");
                }
            } else {
                $dbf->query("SELECT * FROM binarycontent WHERE dataid = '$newhash' ");
                if ($dbf->nf() == 0) {
                    $dbf->query("INSERT INTO binarycontent (dataid, content, filetype) VALUES('$newhash','$contents','$filetype') ");
                }
            }
             
            $status = "File updated!";
        }
        return $status;
    }
     
    function dirList ($directory) {
        global $file_dirlist;
         
        // create a handler for the directory
        $handler = opendir($directory);
         
        // keep going until all files in directory have been read
        while ($file = readdir($handler)) {
            if ($file != '.' && $file != '..') {
                $file_dirlist[] = "$directory/$file";
            }
        }
         
        // tidy up: close the handler
        closedir($handler);
         
        // done!
        return $file_dirlist;
         
    }
    function dirList2 ($directory) {
        $file_dirlist2 = array();
        // create a handler for the directory
        $handler = opendir($directory);
         
        // keep going until all files in directory have been read
        while ($file = readdir($handler)) {
            if ($file != '.' && $file != '..' && !is_dir("$directory$file")) {
                $file_dirlist2[] = "$directory$file";
            }
        }
         
        // tidy up: close the handler
        closedir($handler);
         
        // done!
        return $file_dirlist2;
         
    }
    function recursive_list($directory) {
        global $file_dirlist;
        if (is_dir($directory)) {
            if ($dh = opendir($directory)) {
                while (($file = readdir($dh)) !== false) {
                    if ($file != "." && $file != ".." ) {
                        if (is_dir($directory . $file ) ) {
                            recursive_list($directory . $file . "/" , $file_dir);
                        } else {
                            $file_dirlist[] = "$directory/$file";
                        }
                    }
                     
                }
                closedir($dh);
            }
        }
        return $file_dirlist;
    }
    function fileicon($ext) {
        global $desc, $icon;
        $ext = strtolower($ext);
        switch($ext) {
            case 'dir':
            $desc = 'Directory';
            $icon = 'folder.png';
             break;
            case 'htm':
            $desc = 'HTML Document';
            $icon = 'html.png';
             break;
            case 'html':
            $desc = 'HTML Document';
            $icon = 'html.png';
             break;
            case 'php':
            $desc = 'PHP Script';
            $icon = 'source_php.png';
             break;
            case 'java':
            $desc = 'JAVA Code';
            $icon = 'java.png';
             break;
            case 'css':
            $desc = 'Cascade Style Sheet';
            $icon = 'css.png';
             break;
            case 'doc':
            $desc = 'Word Document';
            $icon = 'wordprocessing.png';
             break;
            case 'xls':
            $desc = 'Excel Spreadsheet';
            $icon = 'spreadsheet.png';
             break;
            case 'ppt':
            $desc = 'PowerPoint Presentation';
            $icon = 'powerpoint.png';
             break;
            case 'rtf':
            $desc = 'RTF File';
            $icon = 'document.png';
             break;
            case 'xml':
            $desc = 'XML Document';
            $icon = 'source.png';
             break;
            case 'jpg':
            $desc = 'JPEG Image';
            $icon = 'image.png';
             break;
            case 'jpe':
            $desc = 'JPEG Image';
            $icon = 'image.png';
             break;
            case 'jpeg':
            $desc = 'JPEG Image';
            $icon = 'image.png';
             break;
            case 'gif':
            $desc = 'GIF Image';
            $icon = 'image.png';
             break;
            case 'bmp':
            $desc = 'Windows Bitmap Image';
            $icon = 'image.png';
             break;
            case 'png':
            $desc = 'PNG Image';
            $icon = 'image.png';
             break;
            case 'tif':
            $desc = 'TIFF Image';
            $icon = 'image.png';
             break;
            case 'tiff':
            $desc = 'TIFF Image';
            $icon = 'image.png';
             break;
            case 'mp3':
            $desc = 'MP3 Audio';
            $icon = 'sound.png';
             break;
            case 'wma':
            $desc = 'WMA Audio';
            $icon = 'sound.png';
             break;
            case 'mid':
            $desc = 'MIDI Sequence';
            $icon = 'midi.png';
             break;
            case 'midi':
            $desc = 'MIDI Sequence';
            $icon = 'midi.png';
             break;
            case 'rmi':
            $desc = 'MIDI Sequence';
            $icon = 'midi.png';
             break;
            case 'au':
            $desc = 'AU Sound';
            $icon = 'sound.png';
             break;
            case 'snd':
            $desc = 'AU Sound';
            $icon = 'sound.png';
             break;
            case 'mpeg':
            $desc = 'MPEG Video';
            $icon = 'video.png';
             break;
            case 'mpg':
            $desc = 'MPEG Video';
            $icon = 'video.png';
             break;
            case 'mpe':
            $desc = 'MPEG Video';
            $icon = 'video.png';
             break;
            case 'wmv':
            $desc = 'Windows Media File';
            $icon = 'video.png';
             break;
            case 'iso':
            $desc = 'CD Image';
            $icon = 'iso.png';
             break;
            case 'avi':
            $desc = 'AVI Video';
            $icon = 'video.png';
             break;
            case 'zip':
            $desc = 'ZIP Archive';
            $icon = 'ark.png';
             break;
            case 'rar':
            $desc = 'RAR Archive';
            $icon = 'ark.png';
             break;
            case 'cab':
            $desc = 'CAB Archive';
            $icon = 'ark.png';
             break;
            case 'gz':
            $desc = 'GZIP Archive';
            $icon = 'tgz.png';
             break;
            case 'tar':
            $desc = 'TAR Archive';
            $icon = 'tar.png';
             break;
            case 'sdw':
            $desc = 'OpenOffice Writer document';
            $icon = 'ooo_writer.png';
             break;
            case 'sxw':
            $desc = 'OpenOffice Writer document';
            $icon = 'ooo_writer.png';
             break;
            case 'sda':
            $desc = 'OpenOffice Draw document';
            $icon = 'ooo_draw.png';
             break;
            case 'sxd':
            $desc = 'OpenOffice Draw document';
            $icon = 'ooo_draw.png';
             break;
            case 'sdc':
            $desc = 'OpenOffice Calc spreadsheet';
            $icon = 'ooo_calc.png';
             break;
            case 'sxc':
            $desc = 'OpenOffice Calc spreadsheet';
            $icon = 'ooo_calc.png';
             break;
            case 'sdd':
            $desc = 'OpenOffice Impress presentation';
            $icon = 'ooo_impress.png';
             break;
            case 'sdp':
            $desc = 'OpenOffice Impress presentation';
            $icon = 'ooo_impress.png';
             break;
            case 'sxi':
            $desc = 'OpenOffice Impress presentation';
            $icon = 'ooo_impress.png';
             break;
            case 'odt':
            $desc = 'OpenDocument word processing document';
            $icon = 'wordprocessing.png';
             break;
            case 'ods':
            $desc = 'OpenDocuments spreadsheet';
            $icon = 'spreadsheet.png';
             break;
            case 'odp':
            $desc = 'OpenDocument presentation';
            $icon = 'ooo_impress.png';
             break;
            case 'odg':
            $desc = 'OpenDocument graphic';
            $icon = 'ooo_draw.png';
             break;
            case 'txt':
            $desc = 'Text Document';
            $icon = 'txt.png';
             break;
            case 'js':
            $desc = 'Javascript Document';
            $icon = 'source.png';
             break;
            case 'sh':
            $desc = 'Shell Script';
            $icon = 'shellscript.png';
             break;
            case 'dll':
            $desc = 'Binary File';
            $icon = 'binary.png';
             break;
            case 'exe':
            $desc = 'Executable File';
            $icon = 'exe.png';
             break;
            case 'pdf':
            $desc = 'Adobe Acrobat Document';
            $icon = 'pdf.png';
             break;
            case 'pl':
            $desc = 'Perl Script';
            $icon = 'source_pl.png';
             break;
            case 'c':
            $desc = 'C Code';
            $icon = 'c.png';
             break;
            case 'h':
            $desc = 'H Code';
            $icon = 'h.png';
             break;
            case 'cgi':
            $desc = 'Perl Script';
            $icon = 'source_pl.png';
             break;
            case 'ps':
            $desc = 'Postscript File';
            $icon = 'postscript.png';
             break;
            case 'dvi':
            $desc = 'DVI File';
            $icon = 'dvi.png';
             break;
            case 'swf':
            $desc = 'Flash';
            $icon = 'flash.png';
             break;
            case 'chm':
            $desc = 'Compiled HTML Help';
            $icon = 'default.png';
             break;
            default:
            $desc = "$ext File";
            $icon = 'default.png';
             break;
        }
    }
?>
